package com.haotunet.app.goldtreasure.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import java.math.BigDecimal;
import java.util.regex.Pattern;

public class Utils {
    public static Boolean isVeriCode(String str) {
        Pattern p = Pattern.compile("^([0-9]{4})");
        return str.matches(p.pattern());
    }

    public static Boolean isCellularPhoneNumber(String str) {
        Pattern p = Pattern.compile("^(1[3|4|5|7|8][0-9])\\d{4,8}$");
        return str.matches(p.pattern());
    }

    public static Boolean isPassword(String str) {
        Pattern p = Pattern.compile("^(?!\\D+$)(?![^a-zA-Z]+$)\\S{6,20}$");
        return str.matches(p.pattern());
    }

    public static Boolean isNumble(String str) {
        Pattern p = Pattern.compile("[0-9]");
        return str.matches(p.pattern());
    }

    public static String hideMidFourPhoneNum(String phone) {
        if (TextUtils.isEmpty(phone) || phone.length() != 11) {
            return "";
        } else {
            return phone.substring(0, phone.length() - (phone.substring(3)).length()) + "****" + phone.substring(7);
        }
    }

    public static String hideRealName(String realName) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < realName.length() - 1; i++) {
            builder.append("*");
        }
        String hideName = realName.substring(0, 1) + builder.toString();
        return hideName;
    }

    public static String hideIdNum(String idNum) {
        if (TextUtils.isEmpty(idNum) || idNum.length() != 18) {
            return "";
        } else {
            return idNum.substring(0, idNum.length() - (idNum.substring(10)).length()) + "********";
        }
    }

    public static boolean isNetworkConnectionActive(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();

        if (networkInfo == null || !networkInfo.isConnected()) {
            return false;
        }
        return true;
    }

    /**
     * 保留3位小数
     *
     * @param value
     * @return
     */
    public static String keepThreeDecimalPlaces(String value) {
        BigDecimal bigDecimal = new BigDecimal(value).setScale(3, BigDecimal.ROUND_HALF_UP);
        return bigDecimal.toString();
    }

    /**
     * 保留2位小数
     *
     * @param value
     * @return
     */
    public static String keepTwoDecimalPlaces(String value) {
        BigDecimal bigDecimal = new BigDecimal(value).setScale(2, BigDecimal.ROUND_HALF_UP);
        return bigDecimal.toString();
    }

    public static String decimalMultiplyKeepTwoPlaces(double a, double b) {
        BigDecimal bda = new BigDecimal(String.valueOf(a));
        BigDecimal bdb = new BigDecimal(String.valueOf(b));
        return new BigDecimal(bda.multiply(bdb).toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }

    public static double decimalReduceKeepTwoPlaces(double a, double b) {
        BigDecimal dba = new BigDecimal(String.valueOf(a));
        BigDecimal dbb = new BigDecimal(String.valueOf(b));
        return new BigDecimal(dba.subtract(dbb).toString()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * a/b保留3位小数
     *
     * @param a
     * @param b
     * @return String
     * @author jason
     * @date 2016年4月21日
     */
    public static String decimalADivideB(double a, double b) {
        BigDecimal ba = new BigDecimal(String.valueOf(a));
        BigDecimal bb = new BigDecimal(String.valueOf(b));
        return ba.divide(bb, 3, BigDecimal.ROUND_HALF_UP).toString();
    }
}
